package recursion;

import java.util.Arrays;

public class BubbleSearch {
    public static void BubbleSearch(int[] arr) {
        recursionBubbleSearch(arr, arr.length - 1);
    }

    /**
     * 递归冒泡改进方法 定义一个变量x 指向排名完以后的low索引 x为0时说明序列全排好
     *
     * @param arr  待排序数组
     * @param high 数组的最后一个元素
     */
    private static void recursionBubbleSearch(int arr[], int high) {
        if (high == 0) {
            return;
        }
        int x = 0;
        for (int low = 0; low < high; low++) {
            if (arr[low] > arr[low + 1]) {
                int temp = arr[low];
                arr[low] = arr[low + 1];
                arr[low + 1] = temp;
                x = low;
            }
        }
        recursionBubbleSearch(arr, x);
    }

    public static void main(String[] args) {
        int[] arr = {5, 4, 23, 343, 1212, 343, 2, 3};
        recursionBubbleSearch(arr, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }
}

